/**
 * 
 */
package com.jm.web.builder;

import java.io.IOException;
import java.util.Iterator;

import com.jm.utils.PageJsGear;
import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.BasePage;
import com.jm.web.form.Input;
import com.jm.web.form.Select;
import com.jm.web.form.Textarea;
import com.jm.web.form.support.FormItem;
import com.jm.web.tags.DialogTag;

/**
 * @author yanjie
 *
 * 2011/9/19 上午9:57:47
 * 搜索表单绘制器
 */
public class DialogBuilder extends BaseBuilder{
	protected DialogTag dialogTag = null;
	
	@Override
	public void RenderStart() {
		if(this.getBaseTag() instanceof DialogTag)
			this.dialogTag = (DialogTag)this.getBaseTag();
		
		this.getBaseTag().getPage().setSpId(this.dialogTag.getId());
		StringBuilderEx sb = new StringBuilderEx();
		
		if(this.dialogTag.GetExtValue("layout",true)){
			sb.Append("<div id=\""+this.dialogTag.getId()+"\" style=\"height: auto;margin-top: 2px;border: %1$s solid #8DB2E3;display:none; %2$s \" >"
					,this.dialogTag.GetExtValue("border","1px")+(this.dialogTag.GetExtValue("border","1px").endsWith("px") ? "" : "px"),
					this.dialogTag.IsContainsKey("width") ? "width:"+this.dialogTag.GetExtValue("width", "") : ""
					);
		}
			
		if(this.dialogTag.getTabSize() > 0){
			sb.Append("\r\n<ul>");
			if(StringHelper.IsNullOrEmpty(this.dialogTag.getCustomtab())){
				if(this.dialogTag.getTabSize() > 0)
					sb.Append("\r\n<li><a href=\"#%1$s-1\">基本</a></li>",this.dialogTag.getId());
				if(this.dialogTag.getTabSize() > 1)
					sb.Append("\r\n<li><a href=\"#%1$s-2\">其它</a></li>",this.dialogTag.getId());
			}else{
				String strCustomTab = this.dialogTag.getCustomtab().replaceAll(",","|").replaceAll(";","|");
				String tabs[] = strCustomTab.split("[|]");
				for (int i = 0; i < tabs.length; i++) {
					sb.Append("\r\n<li><a href=\"#%1$s-%2$s\">%3$s</a></li>",this.dialogTag.getId(),String.valueOf(i+1),tabs[i]);
				}
			}
			sb.Append("\r\n</ul>");
		}
		try {
			this.getOut().write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/* (non-Javadoc)
	 * @see com.jm.web.builder.BaseBuilder#Render()
	 */
	@Override
	public void RenderEnd() {
		if(this.getBaseTag() instanceof DialogTag)
			this.dialogTag = (DialogTag)this.getBaseTag();
		
		StringBuilderEx sb = new StringBuilderEx();
		if(this.dialogTag.GetExtValue("layout",true)){
			sb.Append("</div>");
			try {
				this.getOut().write(sb.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		sb.Reset();
		//1.註冊搜索表單標籤頁配置的JS
		if(this.dialogTag.getTabSize() > 0 || !StringHelper.IsNullOrEmpty(this.dialogTag.getCustomtab())){
			sb.Append(PageJsGear.getLogBebinJs(this.getBaseTag().getPage(),"sp-tab"));
			
			sb.Append("var config={};");
			if(this.dialogTag.GetExtValue("cache",true)){
				sb.Append("if($.cookie('tabindex_%1$s') != null){config['selected']=$.cookie('tabindex_%1$s');} ",this.dialogTag.getId());
			}
			
			sb.Append("config['select']=function(event, ui){$P.tabselect(ui,'%1$s',%2$s)}; ",this.dialogTag.getId(),this.dialogTag.GetExtValue("cache",true));
			sb.Append("$('div[id=%1$s]').tabs(config);",this.dialogTag.getId());
			sb.Append(PageJsGear.getLogEndJs(this.getBaseTag().getPage(),"sp-tab"));
		}
		
		//2.注册dialog 
		sb.Append("var config = {};");
		sb.Append("config['autoOpen']=%1$s;", "false");
		sb.Append("config['modal']=%1$s;", this.dialogTag.GetExtValue("modal","true"));
		
		//按钮
		if(this.dialogTag.IsContainsKey("dbuttons"))
			sb.Append("config['buttons']=%1$s;", this.dialogTag.GetExtValue("dbuttons","{}"));
		//
		if(this.dialogTag.IsContainsKey("dcloseOnEscape"))
			sb.Append("config['closeOnEscape']=%1$s;", this.dialogTag.GetExtValue("dcloseOnEscape","true"));
		
		if(this.dialogTag.IsContainsKey("dwithClose"))
			sb.Append("config['withClose']=%1$s;", this.dialogTag.GetExtValue("dwithClose","true"));
		
		if(this.dialogTag.IsContainsKey("dcloseText"))
			sb.Append("config['closeText']='%1$s';", this.dialogTag.GetExtValue("dcloseText","close"));
		
		if(this.dialogTag.IsContainsKey("dialogClass"))
			sb.Append("config['dialogClass']='%1$s';", this.dialogTag.GetExtValue("dialogClass",""));
		
		if(this.dialogTag.IsContainsKey("dheight"))
			sb.Append("config['height']='%1$s';", this.dialogTag.GetExtValue("dheight","auto"));
		
		if(this.dialogTag.IsContainsKey("dmaxHeight"))
			sb.Append("config['maxHeight']='%1$s';", this.dialogTag.GetExtValue("dmaxHeight","800"));
		
		if(this.dialogTag.IsContainsKey("dminHeight"))
			sb.Append("config['minHeight']='%1$s';", this.dialogTag.GetExtValue("dminHeight","150"));
		
		sb.Append("config['width']='%1$s';", this.dialogTag.GetExtValue("dwidth","400"));
		
		if(this.dialogTag.IsContainsKey("dmaxWidth"))
			sb.Append("config['maxWidth']='%1$s';", this.dialogTag.GetExtValue("dmaxWidth","800"));
		
		if(this.dialogTag.IsContainsKey("dminWidth"))
			sb.Append("config['minWidth']='%1$s';", this.dialogTag.GetExtValue("dminWidth","150"));
		
		if(this.dialogTag.IsContainsKey("dposition")){
			boolean bArray = this.dialogTag.GetExtValue("dposition","center").startsWith("[");
			sb.Append("config['position']=%1$s;",(bArray?"":"'")+this.dialogTag.GetExtValue("dposition","center")+(bArray?"":"'"));
		}
		
		if(this.dialogTag.IsContainsKey("dresizable"))
			sb.Append("config['resizable']=%1$s;", this.dialogTag.GetExtValue("dresizable",true));
		
		if(this.dialogTag.IsContainsKey("dstack"))
			sb.Append("config['stack']=%1$s;", this.dialogTag.GetExtValue("dstack",""));
		
		if(this.dialogTag.IsContainsKey("dtitle"))
			sb.Append("config['title']='%1$s';", this.dialogTag.GetExtValue("dtitle",""));
		
		if(this.dialogTag.IsContainsKey("dzIndex"))
			sb.Append("config['zIndex']='%1$s';", this.dialogTag.GetExtValue("dzIndex","1000"));
		
		sb.Append("$P.dialogs['%1$s']=new $P.dialog($('div[id=%1$s]').dialog(config));",this.dialogTag.getId());
		
		if(this.dialogTag.getItems() != null && this.dialogTag.getItems().size() > 0){
			sb.Append("var _ay = {};");
			for (Iterator it = this.dialogTag.getItems().keySet().iterator();it.hasNext();){
				String key = (String)it.next();
				Object obj = this.dialogTag.getItems().get(key);
				
				if(obj instanceof Input){
					sb.Append("_ay['%1$s']={name:'%1$s',nodename:'%2$s',type:'%3$s'};",key,"input",((Input)obj).getType());
				}else if(obj instanceof Select){
					sb.Append("_ay['%1$s']={name:'%1$s',nodename:'%2$s'};",key,"select");
				}else if(obj instanceof Textarea){
					sb.Append("_ay['%1$s']={name:'%1$s',nodename:'%2$s'};",key,"textarea");
				}else{
					FormItem formitem = (FormItem)obj;
					if("span".equals(formitem.getNodeName()) && formitem.GetExtValue("bind", false)){
						sb.Append("_ay['%1$s']={name:'%1$s',nodename:'%2$s'};",key,"span");
					}
				}
			}
			sb.Append("$P.getdialog('%1$s').items=_ay;",this.dialogTag.getId());
			sb.Append("_ay=null;");
		}
		
		
		
		if(sb.toString().length() > 0){
			if(!this.getBaseTag().getPage().isExport()){
				this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY, sb.toString());
			}
//			try {
//				if(!this.getBaseTag().getPage().isExport()){
//					this.getOut().write("<script>if(true){"+sb.toString()+"}</script>");
//				}
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
		}
		
//		//注册表格页面默认回车事件
//		if(!StringHelper.IsNullOrEmpty(this.getBaseTag().GetExtValue("keydownconfig",""))){
//			this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY, 
//					StringHelper.Format("$P.form.keydownConfig={%1$s};$P.form.regKeydownEvent();",this.getBaseTag().GetExtValue("keydownconfig","")));
//		}
	}
}
